package com.donger.web.mobile.controller.appv2.version;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.donger.business.modules.app.entity.AppVersion;
import com.donger.business.modules.app.service.AppVersionService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.web.mobile.response.CheckAppVersion;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/appv2/version")
@RestController
@AllArgsConstructor
public class VersionController {

    private final AppVersionService appVersionService;



    @ApiOperation(value = "检测App是否有更新")
    @GetMapping("/check")
    public Result check(@RequestParam("buildBuildVersion")
                            @ApiParam("buildBuildVersion")
                                    Integer buildBuildVersion){
        List<AppVersion> list = appVersionService.list(Wrappers.<AppVersion>lambdaQuery()
                .orderByDesc(AppVersion::getBuild)
                .last("limit 1")
        );
        AppVersion appVersion = null;
        // 如果没有  有数据
        CheckAppVersion checkAppVersion = new CheckAppVersion();
        if(CollUtil.isNotEmpty(list)){
            appVersion = list.get(0);
            // 有新版本
            if(buildBuildVersion > appVersion.getBuild()){
                checkAppVersion.setBuildHaveNewVersion(Boolean.TRUE);
            }
            checkAppVersion.setBuildUpdateDescription(appVersion.getDescription());
            checkAppVersion.setBuildVersion(appVersion.getBuild());
            checkAppVersion.setDownloadURL(appVersion.getUrl());
            checkAppVersion.setVersion(appVersion.getVersion());
            return Res.ok(checkAppVersion);
        }else{
            return Res.ok("没有上传APP");
        }
    }


}
